
clear all
************************************
*Numeric Export disabled
local TEX ""
local TEXON 0 //Change "0" to "1" to enable exporting numeric results to .txt files
if !`TEXON' local TEX "*"
*Also need to install `texresults2
*ssc install texresults2
************************************

************************************************
*CEM Simulation Table A-1
************************************************
*Necessary packages
*ssc install cem

***************************************
*Make sure working directory is set
***************************************

*Load data and recode variables
do "Enns_SociologicalScience_Reproduction\LoadGilensDataRecodeConfirmAccuracy.do"

****************
**Generate 90/10 and 90/50 difference variables to be used in cem program
****************

**90/10
quietly: gen dif_90_10=.
quietly: recode dif_90_10 .=1 if (pred90_sw - pred10_sw)>.1
quietly: recode dif_90_10 .=0 if (pred10_sw - pred90_sw)>.1

label var dif_90_10 ">10% dif b/t 90th and 10th income percentiles"

label define diflbl9010 0 "0 10th income percentile more supportive" 1 "1 90th income percentile more supportive"
label values dif_90_10 diflbl9010

**90/50
quietly: gen dif_90_50=.
quietly: recode dif_90_50 .=1 if (pred90_sw - pred50_sw)>.1
quietly: recode dif_90_50 .=0 if (pred50_sw - pred90_sw)>.1

label var dif_90_50 ">10% dif b/t 90th and 50th income percentiles"

label define diflbl9050 0 "0 50th income percentile more supportive" 1 "1 90th income percentile more supportive"
label values dif_90_50 diflbl9050

*******************************************************************
**save data as temporary file to be used for CEM analysis below 
tempfile DS1_cem
save "`DS1_cem'", replace
*******************************************************************

*Save correlation b/t 90th and 10th when a preference gap exists
cor pred90_sw pred10_sw if abs(pred90_sw - pred10_sw)>.1
local cor9010 = r(rho)
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(corntytenprefgap) result(`cor9010') round(2) replace
************************************************
*10v 90 - Balance on 10th percentile
program define cem10_9010

args DS1_cem
use "`DS1_cem'", clear

*drop missing differences (less than abs value(10%))
quietly: drop if dif_90_10==.

quietly: cem pred10_sw if policyadopted!=., tr(dif_90_10) k2k
quietly:logit policyadopted pred10_sw_lor if cem_matched==1

*************
*Correlation b/t 10th and 90th after match
cor pred90_sw pred10_sw if cem_matched==1
scalar cor9010cem = r(rho)
*************
mat def coef = e(b)
scalar V_10th_per = coef[1,1]
scalar V_10th_percons = coef[1,2]
scalar N_10th = e(N)

end

**********************************************
*10 v 90 - Balance on 90th
program define cem90_9010

args DS1_cem
use "`DS1_cem'", clear

*drop missing differences (less than abs value(10%))
quietly: drop if dif_90_10==.

quietly: cem pred90_sw if policyadopted!=., tr(dif_90_10) k2k
quietly: logit policyadopted pred90_sw_lor if cem_matched==1

mat def coef = e(b)
scalar V10_90th_per = coef[1,1]
scalar V10_90th_percons = coef[1,2]
scalar def N10_90th = e(N)

end

************************************************
*50 v 90 - Balance on 50th 
program define cem50_9050
args DS1_cem
use "`DS1_cem'", clear

*drop missing differences (less than abs value(10%))
quietly: drop if dif_90_50==.

quietly: cem pred50_sw if policyadopted!=., tr(dif_90_50) k2k
quietly: logit policyadopted pred50_sw_lor if cem_matched==1

mat def coef = e(b)
scalar V_50th_per = coef[1,1]
scalar V_50th_percons = coef[1,2]
scalar N_50th = e(N)
end

************************************************
*50 v 90 - Balance on 90th 
program define cem90_9050

args DS1_cem
use "`DS1_cem'", clear

*drop missing differences (less than abs value(10%))
quietly: drop if dif_90_50==.

quietly: cem pred90_sw if policyadopted!=., tr(dif_90_50) k2k
quietly: logit policyadopted pred90_sw_lor if cem_matched==1

mat def coef = e(b)
scalar V50_90th_per = coef[1,1]
scalar V50_90th_percons = coef[1,2]
scalar N50_90th = e(N)
end


//set up a new dataset with variable names listed below called cem_data
tempfile cem_data
tempname temp1
postfile `temp1' cor9010cem V_10th_per V_10th_percons N_10th V10_90th_per V10_90th_percons N10_90th ///
				V_50th_per V_50th_percons N_50th V50_90th_per V50_90th_percons N50_90th ///
				using "`cem_data'", replace

set seed 6951929
//run each program 1000 times 
forval i = 1/1000 {

cem10_9010 "`DS1_cem'"

cem90_9010 "`DS1_cem'"

cem50_9050 "`DS1_cem'"

cem90_9050 "`DS1_cem'"

//post results from each simulation to data set 
post `temp1' (cor9010cem) (V_10th_per) (V_10th_percons) (N_10th) (V10_90th_per) (V10_90th_percons) (N10_90th) ///
			(V_50th_per) (V_50th_percons) (N_50th) (V50_90th_per) (V50_90th_percons) (N50_90th)
}

postclose `temp1' //write results to temp dataset

******************************************************************************
*export results 
use "`cem_data'", clear 

local varlist cor9010cem V_10th_per V_10th_percons V10_90th_per V10_90th_percons V_50th_per V_50th_percons V50_90th_per V50_90th_percons

foreach var of local varlist { 
	//generate locals of median and 95% bounds

	_pctile `var', nq(1000)
	local m_`var' = r(r500)
	local lb_`var' = r(r25) 
    local ub_`var' = r(r975) 	
}

foreach var of varlist N_10th N10_90th N_50th N50_90th {
	sum `var'
	local n_`var' = r(mean)
}

//10 v 90 - 10th 
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(nlowtvn) result(`n_N_10th') round(0) append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medlowtvn) result(`m_V_10th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lblowtvn) result(`lb_V_10th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ublowtvn) result(`ub_V_10th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medlowtvncons) result(`m_V_10th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lblowtvncons) result(`lb_V_10th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ublowtvncons) result(`ub_V_10th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medcorntyten) result(`m_cor9010cem') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lbcorntyten) result(`lb_cor9010cem') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ubcorntyten) result(`ub_cor9010cem') append


//10 v 90 - 90th 
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(nhightvn) result(`n_N10_90th') round(0) append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medlhightvn) result(`m_V10_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lbhightvn) result(`lb_V10_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ubhightvn) result(`ub_V10_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medlhightvncons) result(`m_V10_90th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lbhightvncons) result(`lb_V10_90th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ubhightvncons) result(`ub_V10_90th_percons') append



//50 v 90 - 50th
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(nmidfvn) result(`n_N_50th') round(0) append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medmidfvn) result(`m_V_50th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lfivemidfvn) result(`lb_V_50th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ufivemidfvn) result(`ub_V_50th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medmidfvncons) result(`m_V_50th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lfivemidfvncons) result(`lb_V_50th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ufivemidfvncons) result(`ub_V_50th_percons') append



//50 v 90 - 90th 
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(nhighfvn) result(`n_N50_90th') round(0) append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medhighfvn) result(`m_V50_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lfivehighfvn) result(`lb_V50_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ufivehighfvn) result(`ub_V50_90th_per') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(medhighfvncons) result(`m_V50_90th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(lfivehighfvncons) result(`lb_V50_90th_percons') append
`TEX'texresults2 using TxtFiles_NumericalResults\cem.txt, texmacro(ufivehighfvncons) result(`ub_V50_90th_percons') append


